package com.ymatou.iapi.userservice.testcase;

import java.util.Date;
import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.userservice.parameter.UpdateLoginTimeBean;
import com.ymatou.iapi.userservice.parameter.req.CreateUserReq;
import com.ymatou.iapi.userservice.service.UpdateLoginTimeCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.JUserCallService;
import com.ymttest.database.model.Userinfo;
import com.ymttest.database.sqlwapper.JUserWapper;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.YMTDateUtil;

/**
 * 更新登录时间Test FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_UpdateLoginTime {
	private static UpdateLoginTimeBean updatelogintimeBean;
	private static UpdateLoginTimeCall updatelogintimeCall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("更新登录时间Test");
	}

	@Before
	public void caseUp() {
		updatelogintimeBean = new UpdateLoginTimeBean();
		updatelogintimeCall = new UpdateLoginTimeCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(updatelogintimeCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_UpdateLoginTime_001() {
		Logger.start(true, "更新登录时间,成功");
		try {
			CreateUserReq cur = new CreateUserReq();
			cur = JUserCallService.initCreateUserBean(cur);
			Integer uid = JUserCallService.createUserCall(cur);
			JUserWapper ju = new JUserWapper();
			List<Userinfo> lui = ju.selectUserinfoByUid(uid);
			Userinfo ui = lui.get(0);			
			Date oldlogintime = ui.getLastLoginTime();

			updatelogintimeBean.setUserId(uid);
			updatelogintimeCall.setData(updatelogintimeBean);
			updatelogintimeCall.callService();			
			
			Logger.verifyEquals("true", updatelogintimeCall.getString("Success"), "验证Success");
			Logger.verifyEquals("200", updatelogintimeCall.getString("ResponseCode"), "验证ResponseCode");
			//验证登录时间是否更新
			JUserWapper ju2 = new JUserWapper();
			List<Userinfo> lui2 = ju2.selectUserinfoByUid(uid);
			Userinfo ui2 = lui2.get(0);
			Date newlogintime = ui2.getLastLoginTime();
			Logger.verifyNotEquals(newlogintime, oldlogintime, "验证登录时间是否更新");						
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_UpdateLoginTime_002() {
		Logger.start(true, "userid为空,失败");
		try {
			CreateUserReq cur = new CreateUserReq();
			cur = JUserCallService.initCreateUserBean(cur);
			Integer uid = JUserCallService.createUserCall(cur);

			updatelogintimeBean.setUserId(null);
			updatelogintimeCall.setData(updatelogintimeBean);
			updatelogintimeCall.callService();
			
			Logger.verifyEquals("false", updatelogintimeCall.getString("Success"), "验证Success");
			Logger.verifyEquals("100", updatelogintimeCall.getString("ResponseCode"), "验证ResponseCode");
			Logger.verifyIsContains("用户ID未提供", updatelogintimeCall.getString("LastErrorMessage"), "验证LastErrorMessage");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_UpdateLoginTime_003() {
		Logger.start(true, "userid不存在,失败");
		try {
			CreateUserReq cur = new CreateUserReq();
			cur = JUserCallService.initCreateUserBean(cur);
			Integer uid = JUserCallService.createUserCall(cur);

			updatelogintimeBean.setUserId(99999999);
			updatelogintimeCall.setData(updatelogintimeBean);
			updatelogintimeCall.callService();
			
//			Logger.verifyEquals("false", updatelogintimeCall.getString("Success"), "验证Success");
//			Logger.verifyEquals("100000", updatelogintimeCall.getString("ResponseCode"), "验证ResponseCode");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}